<?php 
namespace app\tools\logic;
use think\facade\Db;
use app\common\logic\MemberAccountLogic;
use app\common\logic\RechargeOrderLogic as CommonRechargeOrderLogic;
use app\common\logic\MemberLevelLogic;

//订单
class OrderRechargeLogic{
	//获取订单统计信息
	static function getStatistics(){
		$member_id 	=	input('member_id', 0);
		$keys 		=	input('keys', '');

		$start_time = input('start_time', '');
		$end_time = input('end_time', '');
		$start_time && $start_time .= " 00:00:00";
		$end_time && $end_time .= "23:59:59";

		$fields = "or.pay_type, 
					sum(or.recharge_amount) as total_amount,
					sum(or.level_amount)  as level_amount,
					sum(or.gift_amount) as gift_amount,
					sum(or.first_amount) as first_amount,
					sum(or.number) as number, count(or.id) as num";
		//统计支付成功的订单
		$obj = Db::name('order_recharge')
				->field($fields)
				->alias('or')
				->join('member m','m.id=or.member_id')
				->join('order_third_pay_log ol','ol.out_order_id=or.id and ol.order_type=10')
				->where('or.status', 20);
		//根据指定关键字查询
		if($keys){
			$obj->where('m.user_name|ol.transaction_sn|or.recharge_sn','like','%'.$keys.'%');
		}
		//时间
		if($start_time){
			$obj->where('or.create_time','>=', $start_time);
		}
		if($end_time){
			$obj->where('or.create_time','<=', $end_time);
		}
		//查询指定用户
		if($member_id){
			$obj->where('or.member_id', $member_id);
		}

		$list = $obj->group('or.pay_type')
				->select()->toArray();
		$data = [];
		$data['alipay_amount'] = 0;
		$data['ltc_amount'] = 0;
		$data['usdt_amouont'] = 0;
		$data['total_amount'] = 0;
		$data['total_zs_amount'] = 0;
		$data['order_number'] = 0;
		$data['alipay_zs_amount'] = 0;
		$data['ltc_zs_amount'] = 0;
		$data['usdt_zs_amount'] = 0;
		// print_r($list);
		if($list){
			foreach($list as $v){
				if($v['pay_type']==10){
					$data['alipay_amount'] = round($v['total_amount'],2);
					$data['alipay_zs_amount'] += $v['level_amount'];
					$data['alipay_zs_amount']  += $v['gift_amount'];
					$data['alipay_zs_amount'] += $v['first_amount'];
				}

				if($v['pay_type']==20){
					$data['ltc_amount'] = round($v['total_amount'],2);
					$data['ltc_zs_amount'] += $v['level_amount'];
					$data['ltc_zs_amount']  += $v['gift_amount'];
					$data['ltc_zs_amount'] += $v['first_amount'];
				}

				if($v['pay_type']==30){
					$data['total_amount'] = round($v['total_amount'],2);
					$data['usdt_zs_amount'] += $v['level_amount'];
					$data['usdt_zs_amount']  += $v['gift_amount'];
					$data['usdt_zs_amount'] += $v['first_amount'];
				}
				$data['total_amount'] += $v['total_amount'];
				$data['order_number'] += $v['num'];

				$data['total_zs_amount'] += $v['level_amount'];
				$data['total_zs_amount']  += $v['gift_amount'];
				$data['total_zs_amount'] += $v['first_amount'];
				
			}
		}
		$data['total_zs_amount'] = round($data['total_zs_amount'],2);
		$data['usdt_zs_amount'] = round($data['usdt_zs_amount'],2);
		$data['ltc_zs_amount'] = round($data['ltc_zs_amount'],2);
		$data['alipay_zs_amount'] = round($data['alipay_zs_amount'],2);
		return $data;
	}
	//获取列表
	static function getList(){
		$member_id 	=	input('member_id', 0);
		$keys 		=	input('keys', '');
		$page_size = input('page_size', 10);
		$status = input('status', '');
		
		$start_time = input('start_time', '');
		$end_time = input('end_time', '');
		$start_time && $start_time .= " 00:00:00";
		$end_time && $end_time .= "23:59:59";

		$fields = "or.id, or.member_id,or.recharge_sn, round(or.recharge_amount,2) as recharge_amount,
				   or.number,round(or.level_amount,2) as level_amount, round(or.gift_amount,2) as gift_amount,
				   round(or.first_amount,2) as first_amount,or.pay_type,or.status,or.create_time,or.create_time,
				   m.user_name,ol.transaction_sn,or.trade_number";

		$obj = Db::name('order_recharge')
				->field($fields)
				->alias('or')
				->join('member m','m.id=or.member_id')
				->join('order_third_pay_log ol','ol.out_order_id=or.id and ol.order_type=10');
		//根据指定关键字查询
		if($keys){
			$obj->where('m.user_name|ol.transaction_sn|or.recharge_sn','like','%'.$keys.'%');
		}
		//时间
		if($start_time){
			$obj->where('or.create_time','>=', $start_time);
		}
		if($end_time){
			$obj->where('or.create_time','<=', $end_time);
		}
		//查询指定用户
		if($member_id){
			$obj->where('or.member_id', $member_id);
		}
		//根据订单状态查询
		if($status!==''){
			// echo $status;
			$obj->where('or.status', $status);
		}

		$list = $obj->order('or.id desc')
				->paginate($page_size);
		return $list;
	}

	//待支付订单确认支付
	static function confirmPay(){
		$recharge_sn = input('recharge_sn', '');
		$transaction_sn = input('transaction_sn', '');
		$remark = input('remark', '');
		if(!$recharge_sn){
			return 143;
		}
		//查询此订单
		$info = Db::name('order_recharge')
				->where('recharge_sn', $recharge_sn)
				->find();
		if(!$info){
			return 144;
		}
		//待支付中
		if($info['status']!=10){
			return 145;
		}
		//确认充值金额
		if($info['recharge_amount']<=0){
			return 146;
		}


		//订单支付完成了，改变订单状态，金额充值到账户
		Db::startTrans();
		try {
			//开始充值
	    	$rs = MemberAccountLogic::init()->accountIn($info);
	    	// echo $rs.'A1';
	    	if(!$rs){
	    		Db::rollback();
	    		return 147;
	    	}
	    	//处理充值订单
			$rs2 = CommonRechargeOrderLogic::init()->orderProcess($info, 20, $transaction_sn, $remark);
	    	// echo $rs2.'A2';
	    	if(!$rs2){
	    		Db::rollback();
	    		return 147;
	    	}
			$rs3 = MemberLevelLogic::init()->levelProcess($info['member_id'], $info['recharge_amount']);
			// echo $rs3.'A3';
			if(!$rs3){
	    		Db::rollback();
	    		return 147;
	    	}
			Db::commit();
			//充值成功了，返回信息
			return 0;
		} catch (\Exception $e) {
			// print_r($e->getMessage());
		    // 回滚事务
		    Db::rollback();
		    return 147;
		}
	}
}